﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SPErrorHandler;
using SendMail;
using System.Configuration;
using System.Collections;
using Microsoft.SharePoint.Linq;
using System.Data;
using System.Data.SqlClient;

namespace SPEnum
{
    class EnumFarm
    {
        public void EnumFarmDoc()
        {
            Console.WriteLine("---------------------------------------------------------------------------------------Content Database Informations----------------------------------------------------------------------------------------------------");
            Console.WriteLine(" ");
            EnumWebAppInfo();
            Console.WriteLine(" ");
            Console.WriteLine("---------------------------------------------------------------------------------------Main Version Document Enumeration------------------------------------------------------------------------------------------------");
            EnumMainDocInfo();
            Console.WriteLine(" ");
            Console.WriteLine("---------------------------------------------------------------------------------------Version Document Enumeration-----------------------------------------------------------------------------------------------------");
            EnumDocVersions();
        }

        private static void EnumWebAppInfo()
        {
            try
            {
                SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>();
                Console.WriteLine(String.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-30} | {5,-30}", "Web App", "DB Name", "Sites in DB", "Backup Required MB Size", "SQL Server", "DB ID"));
                foreach (SPWebApplication webapp in service.WebApplications)
                {
                    foreach (SPContentDatabase db in webapp.ContentDatabases)
                    {
                        Console.WriteLine(String.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-30} | {5,-30}", webapp.Name, db.Name, db.Sites.Count, db.DiskSizeRequired / 1024 / 1024, db.Server, db.Id));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

        private static void EnumMainDocInfo()
        {
            try
            {
                DataSet ds = new DataSet();
                SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>();
                foreach (SPWebApplication webapp in service.WebApplications)
                {
                    foreach (SPContentDatabase db in webapp.ContentDatabases)
                    {
                        SqlConnection connection = new SqlConnection("Data Source=" + db.Server + ";Database=" + db.Name + ";Integrated Security=SSPI");
                        string query = "SELECT AllDocs.Extension, COUNT(AllDocs.Extension) as Count, SUM (Size) as Size FROM [dbo].[AllDocs] WHERE AllDocs.DoclibRowId IS NOT NULL AND AllDocs.DeleteTransactionID = 0x AND (AllDocs.Extension= 'txt' OR AllDocs.Extension= 'bmp' OR AllDocs.Extension= 'doc' OR AllDocs.Extension= 'docx' OR AllDocs.Extension= 'jpg' OR AllDocs.Extension= 'jpeg' OR AllDocs.Extension= 'rtf' OR AllDocs.Extension= 'gif' OR AllDocs.Extension= 'tif' OR AllDocs.Extension= 'tiff'  OR AllDocs.Extension= 'png' OR AllDocs.Extension= 'avi' OR AllDocs.Extension= 'mov' OR AllDocs.Extension= 'mp3' OR AllDocs.Extension= 'mp4' OR AllDocs.Extension= 'mpeg' OR AllDocs.Extension= 'mpg' OR AllDocs.Extension= 'wma' OR AllDocs.Extension= 'vsd' OR AllDocs.Extension= 'xlsx' OR AllDocs.Extension= 'xls'  OR AllDocs.Extension= 'ppt' OR AllDocs.Extension= 'pptx' OR AllDocs.Extension= 'accdb'  OR AllDocs.Extension= 'pdf') GROUP BY AllDocs.Extension ORDER BY Size DESC";
                        SqlCommand command = new SqlCommand(query, connection);
                        command.CommandType = CommandType.Text;
                        SqlDataAdapter SdmDataAdapter = new SqlDataAdapter(command);
                        SdmDataAdapter.Fill(ds, "DocSize");
                        DataTable tblAuthors;
                        tblAuthors = ds.Tables["DocSize"];
                        if (ds.Tables["DocSize"].Rows.Count != 0)
                        {
                            Console.WriteLine(" ");
                            Console.WriteLine(String.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-20}", "Web App", "DB Name", "Extension", "Count", "MB Size"));
                            foreach (DataRow drCurrent in tblAuthors.Rows)
                            {
                                Console.WriteLine(string.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-20}", webapp.Name, db.Name, drCurrent["Extension"].ToString(), drCurrent["Count"].ToString(), Math.Round(decimal.Parse(drCurrent["Size"].ToString()) / 1024 / 1024, 2)));
                            }
                        }
                        ds.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

        private static void EnumDocVersions()
        {
            try
            {
                DataSet ds = new DataSet();
                SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>();
                foreach (SPWebApplication webapp in service.WebApplications)
                {
                    foreach (SPContentDatabase db in webapp.ContentDatabases)
                    {
                        SqlConnection connection = new SqlConnection("Data Source=" + db.Server + ";Database=" + db.Name + ";Integrated Security=SSPI");
                        string query = "SELECT AllDocs.Extension, COUNT(AllDocs.Extension) as Count, SUM (Size) as Size FROM [dbo].[AllDocs] WHERE AllDocs.DoclibRowId IS NOT NULL AND AllDocs.DeleteTransactionID = 0x AND (AllDocs.Extension= 'txt' OR AllDocs.Extension= 'bmp' OR AllDocs.Extension= 'doc' OR AllDocs.Extension= 'docx' OR AllDocs.Extension= 'jpg' OR AllDocs.Extension= 'rtf' OR AllDocs.Extension= 'gif')  GROUP BY AllDocs.Extension  ORDER BY Size DESC";
                        SqlCommand command = new SqlCommand(query, connection);
                        command.CommandType = CommandType.Text;
                        SqlDataAdapter SdmDataAdapter = new SqlDataAdapter(command);
                        SdmDataAdapter.Fill(ds, "DocVersion");
                        DataTable tblAuthors;
                        tblAuthors = ds.Tables["DocVersion"];
                        if (ds.Tables["DocVersion"].Rows.Count != 0)
                        {
                            Console.WriteLine(" ");
                            Console.WriteLine(String.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-20}", "Web App", "DB Name", "Extension", "Count", "MB Size"));
                            foreach (DataRow drCurrent in tblAuthors.Rows)
                            {
                                Console.WriteLine(string.Format("{0,-30} | {1,-40} | {2,-11} | {3,-23} | {4,-20}", webapp.Name, db.Name, drCurrent["Extension"].ToString(), drCurrent["Count"].ToString(), Math.Round(decimal.Parse(drCurrent["Size"].ToString()) / 1024 / 1024, 2)));
                            }
                        }
                        ds.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }
    }
}
